﻿
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_Flow_FinishDocument_V2]') AND type in (N'P', N'PC'))
    DROP PROCEDURE [dbo].[sproc_Flow_FinishDocument_V2];
GO
CREATE PROCEDURE [dbo].[sproc_Flow_FinishDocument_V2]
    @DocID int
AS
BEGIN
SET NOCOUNT ON;
SET XACT_ABORT ON;

IF ( @@TRANCOUNT = 0 )
BEGIN
    RAISERROR(N'This sproc must be executed within an active transaction.', 16, 1);
    RETURN -1;
END

--删除所有的状态
DELETE FROM dbo.uds_flow_status WHERE doc_id = @DocID;

--删除真正的消息
DELETE FROM dbo.UDS_SMS_MobileMsgSendBuffer
    WHERE msgid in (
        SELECT msgid
            FROM dbo.uds_flow_message
            WHERE doc_id = @docid
    );


--删除当前文档的提醒
DELETE FROM dbo.uds_flow_message WHERE doc_id = @docid;

--设置标记
UPDATE dbo.uds_flow_document SET doc_status = /* Finished */3 WHERE doc_id = @DocID;

END
GO
